home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / reve / sunview.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  22.2 KB  |  786 lines

  1. /*LINTLIBRARY*/
  2.  
  3. /*  @(#)sunview.c 1.20 91/11/13
  4.  *
  5.  *  SunView dependent graphics routines used by reve.
  6.  *
  7.  *  Copyright (C) 1990, 1991 - Rich Burridge & Yves Gallot.
  8.  *  All rights reserved.
  9.  *
  10.  *  Permission is granted to copy this source, for redistribution
  11.  *  in source form only, provided the news headers in "substantially
  12.  *  unaltered format" are retained, the introductory messages are not
  13.  *  removed, and no monies are exchanged.
  14.  *
  15.  *  Permission is also granted to copy this source, without the
  16.  *  news headers, for the purposes of making an executable copy by
  17.  *  means of compilation, provided that such copy will not be used
  18.  *  for the purposes of competition in any othello tournaments, without
  19.  *  prior permission from the authors.
  20.  *
  21.  *  No responsibility is taken for any errors or inaccuracies inherent
  22.  *  either to the comments or the code of this program, but if reported
  23.  *  (see README file), then an attempt will be made to fix them.
  24.  */
  25.  
  26. #include "reve.h"
  27. #include "color.h"
  28. #include "extern.h"
  29. #include "images.h"
  30. #include <sys/file.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/wait.h>
  33. #include <suntool/sunview.h>
  34. #include <suntool/canvas.h>
  35.  
  36. enum gr_type gtype = GSUNVIEW ;      /* Graphics type. */
  37.  
  38. #define  CLIENT_NO           10
  39.  
  40. #define  CURSOR_SET          (void) cursor_set
  41. #define  ICON_SET            (void) icon_set
  42. #define  MENU_SET            (void) menu_set
  43. #define  PW_LOCK             (void) pw_lock
  44. #define  PW_ROP              (void) pw_rop
  45. #define  PW_SETCMSNAME       (void) pw_setcmsname
  46. #define  PW_STENCIL          (void) pw_stencil
  47. #define  PW_PUTCOLORMAP      (void) pw_putcolormap
  48. #define  PW_REVERSEVIDEO     (void) pw_reversevideo
  49. #define  PW_TTEXT            (void) pw_ttext
  50. #define  PW_UNLOCK           (void) pw_unlock
  51. #define  PW_VECTOR           (void) pw_vector
  52. #define  PW_WRITEBACKGROUND  (void) pw_writebackground
  53. #define  WIN_BELL            (void) win_bell
  54. #define  WINDOW_DONE         (void) window_done
  55. #define  WINDOW_SET          (void) window_set
  56.  
  57. #define  BOARDFONT           "/usr/lib/fonts/fixedwidthfonts/cour.r.10"
  58. #define  BOLDFONT            "/usr/lib/fonts/fixedwidthfonts/screen.b.12"
  59. #define  HELPFONT            "/usr/lib/fonts/fixedwidthfonts/cour.r.10"
  60. #define  NORMALFONT          "/usr/lib/fonts/fixedwidthfonts/screen.r.12"
  61.  
  62. #define  ROOT_NAME           "/dev/win0"
  63.  
  64. Pixrect *hglass_pr, *icon_pr, *nocur_pr ;
  65.  
  66. Canvas ccanvas, gcanvas, hcanvas, pcanvas ;
  67. Cursor cursor[MAXCURSORS] ;
  68. Event *cur_event ;
  69. Frame cframe, gframe, hframe, pframe ;
  70. Icon reve_icon ;
  71.  
  72. Notify_client client = (Notify_client) CLIENT_NO ;
  73.  
  74. static void canvas_proc            P((Canvas, Event *)) ;
  75. static void load_colors            P(()) ;
  76.  
  77. static Notify_value check_clock    P((Notify_client, int)) ;
  78. static Notify_value quit_proc      P((Notify_client, Destroy_status)) ;
  79. static Notify_value read_from_fd   P((Notify_client, int)) ;
  80. static Notify_value sigchldcatcher P((Notify_client, int,
  81.                                       union wait *, struct rusage *)) ;
  82. static Pixfont *get_font           P((char *)) ;
  83. static Pixrect *load_image         P((int, int, unsigned char *)) ;
  84. static Pixwin *get_wtype           P((enum win_type)) ;
  85.  
  86. Pixfont *font[MAXFONTS] ;
  87. Pixrect *images[MAXIMAGES] ;
  88. Pixwin *cpw, *gpw, *hpw, *ppw ;
  89.  
  90. void pw_batch() ;
  91. int opvals[3] ;         /* Pixrect rasterop values. */
  92. int pid ;
  93. static int rootfd ;
  94.  
  95. /*  256-byte table for quickly reversing the bits in an unsigned 8-bit char,
  96.  *  used to convert between MSBFirst and LSBFirst image formats.
  97.  */
  98.  
  99. char revtable[256] = {
  100.         0, -128,   64,  -64,   32,  -96,   96,  -32,
  101.        16, -112,   80,  -48,   48,  -80,  112,  -16,
  102.         8, -120,   72,  -56,   40,  -88,  104,  -24,
  103.        24, -104,   88,  -40,   56,  -72,  120,   -8,
  104.         4, -124,   68,  -60,   36,  -92,  100,  -28,
  105.        20, -108,   84,  -44,   52,  -76,  116,  -12,
  106.        12, -116,   76,  -52,   44,  -84,  108,  -20,
  107.        28, -100,   92,  -36,   60,  -68,  124,   -4,
  108.         2, -126,   66,  -62,   34,  -94,   98,  -30,
  109.        18, -110,   82,  -46,   50,  -78,  114,  -14,
  110.        10, -118,   74,  -54,   42,  -86,  106,  -22,
  111.        26, -102,   90,  -38,   58,  -70,  122,   -6,
  112.         6, -122,   70,  -58,   38,  -90,  102,  -26,
  113.        22, -106,   86,  -42,   54,  -74,  118,  -10,
  114.        14, -114,   78,  -50,   46,  -82,  110,  -18,
  115.        30,  -98,   94,  -34,   62,  -66,  126,   -2,
  116.         1, -127,   65,  -63,   33,  -95,   97,  -31,
  117.        17, -111,   81,  -47,   49,  -79,  113,  -15,
  118.         9, -119,   73,  -55,   41,  -87,  105,  -23,
  119.        25, -103,   89,  -39,   57,  -71,  121,   -7,
  120.         5, -123,   69,  -59,   37,  -91,  101,  -27,
  121.        21, -107,   85,  -43,   53,  -75,  117,  -11,
  122.        13, -115,   77,  -51,   45,  -83,  109,  -19,
  123.        29,  -99,   93,  -35,   61,  -67,  125,   -3,
  124.         3, -125,   67,  -61,   35,  -93,   99,  -29,
  125.        19, -109,   83,  -45,   51,  -77,  115,  -13,
  126.        11, -117,   75,  -53,   43,  -85,  107,  -21,
  127.        27, -101,   91,  -37,   59,  -69,  123,   -5,
  128.         7, -121,   71,  -57,   39,  -89,  103,  -25,
  129.        23, -105,   87,  -41,   55,  -73,  119,   -9,
  130.        15, -113,   79,  -49,   47,  -81,  111,  -17,
  131.        31,  -97,   95,  -33,   63,  -65,  127,   -1,
  132. } ;
  133.  
  134. void
  135. batch(state)            /* Turn graphics batching on or off. */
  136. enum bltype state ;
  137. {
  138.   switch (state)
  139.     {
  140.       case IS_ON  : pw_batch_on(cpw) ;
  141.                     break ;
  142.       case IS_OFF : pw_batch_off(cpw) ;
  143.     }
  144. }
  145.  
  146.  
  147. void
  148. beep()
  149. {
  150.   static struct timeval btime = { 0, 250000 } ;   /* Beep timer. */
  151.   int bfd ;        /* File descriptor for reve window. */
  152.  
  153.   bfd = (int) window_get(gframe, WIN_FD) ;
  154.   WIN_BELL(bfd, btime, 0) ;
  155. }
  156.  
  157.  
  158. /*ARGSUSED*/
  159. static void
  160. canvas_proc(c, event)
  161. Canvas c ;
  162. Event *event ;
  163. {
  164.        if (c == ccanvas) curwin = W_PANEL ;
  165.   else if (c == gcanvas) curwin = W_BOARD ;
  166.   else if (c == hcanvas) curwin = W_HELP ;
  167.   else if (c == pcanvas) curwin = W_PROPS ;
  168.  
  169.   cur_event = event ;     /* Determine what kind of event it is. */
  170.   handle_event() ;        /* And do the appropriate action. */
  171.   update_clock(next_player, FALSE) ;
  172. }
  173.  
  174.  
  175. static Notify_value
  176. check_clock(client, itimer_type)
  177. Notify_client client ;
  178. int itimer_type ;
  179. {
  180.   update_clock(next_player, FALSE) ;
  181.   return(NOTIFY_DONE) ;
  182. }
  183.  
  184.  
  185. void
  186. close_reve()
  187. {
  188.   WINDOW_SET(gframe, FRAME_CLOSED, TRUE, 0) ;
  189. }
  190.  
  191.  
  192. void
  193. color_area(wtype, x, y, width, height, color)
  194. enum win_type wtype ;
  195. int x, y, width, height, color ;
  196. {
  197.   Pixwin *pw = get_wtype(wtype) ;
  198.  
  199.   PW_WRITEBACKGROUND(pw, x, y, width, height, PIX_SRC | PIX_COLOR(color)) ;
  200. }
  201.  
  202.  
  203. void
  204. connect_io()    /* Connect to computer process and possible remote human. */
  205. {
  206.   if (pipe_io[1][0] > 0)
  207.     notify_set_input_func(client, read_from_fd, pipe_io[1][0]) ;
  208.   if (socketfd > 0)
  209.     notify_set_input_func(client, read_from_fd, socketfd) ;
  210.   notify_set_wait3_func(client, sigchldcatcher, pid) ;
  211. }
  212.  
  213.  
  214. void
  215. destroy_reve()         /* Terminate Reve. */
  216. {
  217.   window_destroy(cframe) ;
  218.   window_destroy(gframe) ;
  219.   KILL(pid, SIGKILL) ;
  220.   exit(0) ;
  221. }
  222.  
  223.  
  224. void
  225. draw_image(wtype, x, y, width, height, image)
  226. enum win_type wtype ;
  227. int x, y, width, height ;               
  228. enum image_type image ;   
  229. {                        
  230.   Pixwin *pw = get_wtype(wtype) ;
  231.  
  232.   PW_ROP(pw, x, y, width, height, PIX_SRC | PIX_DST,
  233.          images[(int) image], 0, 0) ;
  234. }
  235.  
  236.  
  237. void
  238. draw_line(wtype, x1, y1, x2, y2, op, color)
  239. enum win_type wtype ;
  240. int x1, y1, x2, y2, color ;
  241. enum optype op ;
  242. {
  243.   int rop ;
  244.   Pixwin *pw = get_wtype(wtype) ;
  245.  
  246.   rop = opvals[(int) op] ;
  247.   if (!iscolor && color == C_WHITE) rop = opvals[(int) RCLR] ;
  248.   PW_VECTOR(pw, x1, y1, x2, y2, rop | PIX_COLOR(color), 1) ;
  249. }
  250.  
  251.  
  252. void
  253. draw_stencil(wtype, x, y, width, height, op, color, stencil, image)
  254. enum win_type wtype ;
  255. int x, y, width, height, color ;
  256. enum optype op ;
  257. enum image_type stencil, image ;
  258. {
  259.   int rop ;
  260.   Pixwin *pw = get_wtype(wtype) ;
  261.  
  262.   rop = opvals[(int) op] | PIX_COLOR(color) ;
  263.   PW_STENCIL(pw, x, y, width, height, rop,
  264.              images[(int) stencil], 0, 0, images[(int) image], 0, 0) ;
  265. }
  266.  
  267.  
  268. void
  269. draw_text(wtype, x, y, ftype, color, str)
  270. enum win_type wtype ;
  271. enum font_type ftype ;
  272. int x, y, color ;
  273. char *str ;
  274. {
  275.   Pixwin *pw = get_wtype(wtype) ;
  276.  
  277.   PW_TTEXT(pw, x, y, PIX_SRC | PIX_COLOR(color), font[(int) ftype], str) ;
  278. }
  279.  
  280.  
  281. static Pixfont *
  282. get_font(name)
  283. char *name ;
  284. {
  285.   Pixfont *f ;
  286.  
  287.   f = pf_open(name) ;
  288.   if (f == NULL) f = pf_default() ;
  289.   if (f == NULL)
  290.     {
  291.       perror("couldn't get the default font.") ;
  292.       exit(1) ;
  293.     }
  294.   return f ;
  295. }
  296.  
  297.  
  298. /*ARGSUSED*/
  299. char *
  300. get_resource(rtype)      /* Null routine (currently only X11 and XView). */
  301. enum res_type rtype ;
  302. {
  303.   return((char *) NULL) ;
  304. }
  305.  
  306.  
  307. int
  308. get_strwidth(ftype, str)    /* Get width in pixels of string value. */
  309. enum font_type ftype ;
  310. char *str ;
  311. {
  312.   struct pr_size size ;
  313.  
  314.   size = pf_textwidth(strlen(str), font[(int) ftype], str) ;
  315.   return(size.x) ;
  316. }
  317.  
  318.  
  319. static Pixwin *
  320. get_wtype(wtype)
  321. enum win_type wtype ;
  322. {
  323.        if (wtype == W_PANEL) return(cpw) ;
  324.   else if (wtype == W_BOARD) return(gpw) ;
  325.   else if (wtype == W_HELP)  return(hpw) ;
  326.   else if (wtype == W_PROPS) return(ppw) ;
  327. }
  328.  
  329.  
  330. void
  331. init_fonts()         /* Open the normal and bold fonts. */
  332. {
  333.   int i ;
  334.  
  335.   font[(int) BFONT] = get_font(BOLDFONT) ;
  336.   font[(int) GFONT] = get_font(BOARDFONT) ;
  337.   font[(int) HFONT] = get_font(HELPFONT) ;
  338.   font[(int) NFONT] = get_font(NORMALFONT) ;
  339.  
  340.   for (i = 0; i < MAXFONTS; i++)
  341.     font_heights[i] = font[i]->pf_defaultsize.y ;
  342. }
  343.  
  344.  
  345. /*ARGSUSED*/
  346. void
  347. init_graphics(argc, argv)
  348. int *argc ;
  349. char *argv[] ;
  350. {
  351.   images[(int) BUT_STENCIL] = load_image(64, 64, Sbutton_bits) ;
  352.   images[(int) BUT_INVERT]  = load_image(64, 64, Ibutton_bits) ;
  353.   images[(int) BUT_NORMAL]  = load_image(64, 64, Nbutton_bits) ;
  354.   images[(int) CY_NORMAL]   = load_image(64, 64, Ncycle_bits) ;
  355.   images[(int) CY_STENCIL]  = load_image(64, 64, Scycle_bits) ;
  356.   images[(int) CY_LINVERT]  = load_image(64, 64, Lcycle_bits) ;
  357.   images[(int) CY_RINVERT]  = load_image(64, 64, Rcycle_bits) ;
  358.   icon_pr                   = load_image(64, 64, reve_bits) ;
  359.   images[(int) TOGGLE_OFF]  = load_image(64, 64, Sch_off_bits) ;
  360.   images[(int) TOGGLE_ON]   = load_image(64, 64, Sch_on_bits) ;
  361.   images[(int) P_WHITE]     = load_image(64, 64, white_bits) ;
  362.   images[(int) P_BLACK]     = load_image(64, 64, black_bits) ;
  363.   images[(int) S_MOVE]      = load_image(64, 64, move_bits) ;
  364.   images[(int) S_SUGGEST]   = load_image(64, 64, suggest_bits) ;
  365.  
  366.   hglass_pr                 = load_image(16, 16, hglass_bits) ;
  367.   nocur_pr                  = load_image(16, 16, nocur_bits) ;
  368. }
  369.  
  370.  
  371. int
  372. init_ws_type()
  373. {
  374.   if (getenv("WINDOW_PARENT") == NULL)
  375.     {
  376.       FPRINTF(stderr,"%s: Not a native SunView window\n", progname) ;
  377.       return(-1) ;
  378.     }
  379.   gtype      = GSUNVIEW ;   /* Graphics type. */
  380.   move_delta = 5 ;          /* Delta for computer animate move. */
  381.   rootfd     = open(ROOT_NAME, O_RDONLY, 0) ;
  382.   return(0) ;
  383. }
  384.  
  385.  
  386. static void
  387. load_colors()      /* Create and load reve color map. */
  388. {
  389.   Pixwin *frame_pw ;
  390.   unsigned char r[REVE_COLORSIZE], g[REVE_COLORSIZE], b[REVE_COLORSIZE] ;
  391.   char colorname[CMS_NAMESIZE] ;
  392.   int i ;
  393.  
  394.   for (i = 0; i < REVE_COLORSIZE; i++)
  395.     {
  396.       r[i] = (unsigned char) rcols[i] ;
  397.       g[i] = (unsigned char) gcols[i] ;
  398.       b[i] = (unsigned char) bcols[i] ;
  399.     }
  400.  
  401.   iscolor = (cpw->pw_pixrect->pr_depth == 8) ? 1 : 0 ;
  402.   if (iscolor)
  403.     {
  404.       SPRINTF(colorname, "%s%D", REVE_COLOR, getpid()) ;
  405.       PW_SETCMSNAME(cpw, colorname) ;
  406.  
  407.       PW_PUTCOLORMAP(cpw, 0, REVE_COLORSIZE, r, g, b) ;
  408.     }
  409.   if (inv_video) PW_REVERSEVIDEO(cpw, 0, REVE_COLORSIZE) ;
  410.  
  411.   if (iscolor)
  412.     {
  413.       Pixwin *frame_pw ;
  414.  
  415.       frame_pw = (Pixwin *) window_get(gframe, WIN_PIXWIN) ;
  416.       PW_SETCMSNAME(frame_pw, colorname) ;
  417.       PW_PUTCOLORMAP(frame_pw, 0, REVE_COLORSIZE, r, g, b) ;
  418.  
  419.       PW_SETCMSNAME(gpw, colorname) ;
  420.       PW_PUTCOLORMAP(gpw, 0, REVE_COLORSIZE, r, g, b) ;
  421.  
  422.       PW_SETCMSNAME(hpw, colorname) ;
  423.       PW_PUTCOLORMAP(hpw, 0, REVE_COLORSIZE, r, g, b) ;
  424.  
  425.       PW_SETCMSNAME(ppw, colorname) ;
  426.       PW_PUTCOLORMAP(ppw, 0, REVE_COLORSIZE, r, g, b) ;
  427.     }
  428. }
  429.  
  430.  
  431. static Pixrect *
  432. load_image(width, height, cbuf)
  433. int width, height ;
  434. unsigned char cbuf[] ;
  435. {
  436.   int i, j, llen ;
  437.   unsigned short *sbuf ;
  438.  
  439.   llen = (width * height) / 16 ;
  440.   sbuf = (unsigned short *) malloc((unsigned int) (llen * 2)) ;
  441.   for (i = 0; i < llen; i++)
  442.     sbuf[i] =  (revtable[cbuf[(i*2)+1]] & 0xFF) +
  443.               ((revtable[cbuf[i*2]]     & 0xFF) << 8) ;
  444.  
  445.   return(mem_point(width, height, 1, sbuf)) ;
  446. }
  447.  
  448.  
  449. void
  450. load_resources()     /* Dummy routine; used with X11 and XView versions. */
  451. {
  452. }
  453.  
  454.  
  455. void
  456. lock_screen(state)   /* Turn graphics locking on or off. */
  457. enum bltype state ;
  458. {
  459.   static struct rect r = { 0, 0, 0, 0 } ;
  460.  
  461.   r.r_width  = board_width ;
  462.   r.r_height = board_height ;
  463.   switch (state)
  464.     {
  465.       case IS_ON  : PW_LOCK(gpw, &r) ;
  466.                     break ;
  467.       case IS_OFF : PW_UNLOCK(gpw) ;
  468.     }
  469. }
  470.  
  471.  
  472. void
  473. make_canvas()               /* Create canvas for game board. */
  474. {
  475.   ccanvas = window_create(cframe,           CANVAS,
  476.                           CANVAS_RETAINED, FALSE,
  477.                           WIN_HEIGHT,      PANEL_HEIGHT,
  478.                           WIN_WIDTH,       PANEL_WIDTH,
  479.                           WIN_CONSUME_PICK_EVENTS,
  480.                             MS_LEFT, MS_MIDDLE,
  481.                             LOC_MOVE, LOC_DRAG, LOC_TRAJECTORY,
  482.                             LOC_WINENTER, LOC_WINEXIT,
  483.                             0,
  484.                           WIN_CONSUME_KBD_EVENTS,
  485.                             KBD_USE, KBD_DONE, WIN_ASCII_EVENTS, WIN_UP_EVENTS,
  486.                             0,
  487.                           WIN_EVENT_PROC, canvas_proc,
  488.                           0) ;
  489.   cpw = (Pixwin *) window_get(ccanvas, CANVAS_PIXWIN) ;
  490.   if (inv_video) PW_REVERSEVIDEO(cpw, 0, REVE_COLORSIZE) ;
  491.  
  492.   gcanvas = window_create(gframe,          CANVAS,
  493.                           CANVAS_RETAINED, FALSE,
  494.                           WIN_HEIGHT,      BOARD_DIM,
  495.                           WIN_WIDTH,       BOARD_DIM,
  496.                           WIN_CONSUME_PICK_EVENTS,
  497.                             MS_LEFT, MS_MIDDLE,
  498.                             0,
  499.                           WIN_CONSUME_KBD_EVENTS,
  500.                             KBD_USE, KBD_DONE, WIN_ASCII_EVENTS, WIN_UP_EVENTS,
  501.                             0,
  502.                           WIN_EVENT_PROC, canvas_proc,
  503.                           0) ;
  504.   gpw = (Pixwin *) window_get(gcanvas, CANVAS_PIXWIN) ;
  505.   if (inv_video) PW_REVERSEVIDEO(gpw, 0, REVE_COLORSIZE) ;
  506.  
  507.   pcanvas = window_create(pframe,          CANVAS,
  508.                           CANVAS_RETAINED, FALSE,
  509.                           WIN_HEIGHT,      PROPS_HEIGHT,
  510.                           WIN_WIDTH,       PROPS_WIDTH,
  511.                           WIN_CONSUME_PICK_EVENTS,
  512.                             MS_LEFT, MS_MIDDLE,
  513.                             0,
  514.                           WIN_CONSUME_KBD_EVENTS,
  515.                             KBD_USE, KBD_DONE, WIN_ASCII_EVENTS, WIN_UP_EVENTS,
  516.                             0,
  517.                           WIN_EVENT_PROC, canvas_proc,
  518.                           0) ;
  519.   ppw = (Pixwin *) window_get(pcanvas, CANVAS_PIXWIN) ;
  520.   if (inv_video) PW_REVERSEVIDEO(ppw, 0, REVE_COLORSIZE) ;
  521.  
  522.   cursor[(int) CANVASCUR] = window_get(gcanvas, WIN_CURSOR) ;
  523.   CURSOR_SET(cursor[(int) CANVASCUR], CURSOR_OP, PIX_SRC ^ PIX_DST, 0) ;
  524.   cursor[(int) HOURGLASS] = cursor_create(CURSOR_IMAGE, &hglass_pr, 0) ;
  525.   CURSOR_SET(cursor[(int) HOURGLASS], CURSOR_OP, PIX_SRC ^ PIX_DST, 0) ;
  526.   cursor[(int) NOCURSOR]  = cursor_create(CURSOR_IMAGE, &nocur_pr, 0) ;
  527.   CURSOR_SET(cursor[(int) NOCURSOR], CURSOR_OP, PIX_SRC ^ PIX_DST, 0) ;
  528.   if (!monochrome) load_colors() ;
  529.  
  530.   pid = fork_child() ;
  531. }
  532.  
  533.  
  534. void
  535. make_frame(argc, argv)      /* Create reve window. */
  536. int argc ;
  537. char *argv[] ;
  538. {
  539.   opvals[(int) RCLR] = PIX_CLR ;
  540.   opvals[(int) RSRC] = PIX_SRC ;
  541.   opvals[(int) RINV] = PIX_SRC ^ PIX_DST ;
  542.  
  543.   gframe = window_create((Window) NULL,     FRAME,
  544.                           FRAME_ICON,       reve_icon,
  545.                           FRAME_LABEL,      "reve game board",
  546.                           FRAME_NO_CONFIRM, TRUE,
  547.                           WIN_ERROR_MSG,    "Can't create window.",
  548.                           WIN_X,            0,
  549.                           WIN_Y,            0,
  550.                           FRAME_ARGS,       argc, argv,
  551.                           0) ;
  552.  
  553.   cframe = window_create((Window) NULL,    FRAME,
  554.                          FRAME_ICON,       reve_icon,
  555.                          FRAME_LABEL,      line,
  556.                          FRAME_NO_CONFIRM, TRUE,
  557.                          WIN_ERROR_MSG,    "Can't create window.",
  558.                          WIN_BELOW,        gframe,
  559.                          WIN_SHOW,         TRUE,
  560.                          FRAME_ARGS,       argc, argv,
  561.                          0) ;
  562.  
  563.   pframe = window_create(gframe,           FRAME,
  564.                          FRAME_LABEL,      "reve properties",
  565.                          FRAME_NO_CONFIRM, TRUE,
  566.                          WIN_ERROR_MSG,    "Can't create window.",
  567.                          WIN_RIGHT_OF,     gframe,
  568.                          WIN_SHOW,         FALSE,
  569.                          FRAME_ARGS,       argc, argv,
  570.                          0) ;
  571. }
  572.  
  573.  
  574. void
  575. make_help_window(argc, argv)
  576. int argc ;
  577. char *argv[] ;
  578. {
  579.   int fontwidth ;
  580.  
  581.   hframe = window_create(gframe,           FRAME,
  582.                          FRAME_LABEL,      "reve help",
  583.                          FRAME_NO_CONFIRM, TRUE,
  584.                          WIN_ERROR_MSG,    "Can't create window.",
  585.                          WIN_RIGHT_OF,     gframe,
  586.                          WIN_SHOW,         FALSE,
  587.                          FRAME_ARGS,       argc, argv,
  588.                          0) ;
  589.  
  590.   fontwidth = font[(int) HFONT]->pf_defaultsize.x ;
  591.   help_height = ((font_heights[(int) HFONT] + 1) * HELP_ROWS) +
  592.                 (4 * CGAP) + CHEIGHT ;
  593.   help_width  = (fontwidth * HELP_COLS) + (2 * CGAP) ;
  594.  
  595.   hcanvas = window_create(hframe,          CANVAS,
  596.                           CANVAS_RETAINED, FALSE,
  597.                           WIN_HEIGHT,      help_height,
  598.                           WIN_WIDTH,       help_width,
  599.                           WIN_CONSUME_PICK_EVENTS,
  600.                             MS_LEFT, MS_MIDDLE,
  601.                             0,
  602.                           WIN_CONSUME_KBD_EVENTS,
  603.                             KBD_USE, KBD_DONE, WIN_ASCII_EVENTS, WIN_UP_EVENTS,
  604.                             0,
  605.                           WIN_EVENT_PROC, canvas_proc,
  606.                           0) ;
  607.   hpw = (Pixwin *) window_get(hcanvas, CANVAS_PIXWIN) ;
  608.   if (inv_video) PW_REVERSEVIDEO(hpw, 0, REVE_COLORSIZE) ;
  609. }
  610.  
  611.  
  612. void
  613. make_icon()
  614. {
  615.   reve_icon = icon_create(ICON_IMAGE, icon_pr, 0) ;
  616. }
  617.  
  618.  
  619. void
  620. make_pieces(width, height)
  621. int width, height ;
  622. {
  623.   bborder = BBORDER ;
  624.  
  625.   cell_width   = (width  - (2 * bborder)) / BOARD_SIZE ;
  626.   cell_height  = (height - (2 * bborder)) / BOARD_SIZE ;
  627.  
  628.   pieceXmargin = cell_width  / 8 ;
  629.   pieceYmargin = cell_height / 8 ;
  630.  
  631.   pieceXrad = (cell_width  - (2 * pieceXmargin)) / 2 ;
  632.   if (pieceXrad > MAXPRAD)
  633.     {
  634.       pieceXrad    = MAXPRAD ;
  635.       pieceXmargin = (cell_width - (2 * pieceXrad)) / 2 ;
  636.     }
  637.  
  638.   pieceYrad = (cell_height - (2 * pieceYmargin)) / 2 ;
  639.   if (pieceYrad > MAXPRAD)
  640.     {
  641.       pieceYrad    = MAXPRAD ;
  642.       pieceYmargin = (cell_height - (2 * pieceYrad)) / 2 ;
  643.     }
  644. }
  645.  
  646.  
  647. void
  648. open_reve()
  649. {
  650.   WINDOW_SET(gframe, FRAME_CLOSED, FALSE, 0) ;
  651. }
  652.  
  653.  
  654. void
  655. process_event()       /* Process the next canvas event. */
  656. {
  657.   int id ;
  658.  
  659.   id   = (event_id(cur_event)) ;
  660.   curx = event_x(cur_event) ;
  661.   cury = event_y(cur_event) ;
  662.  
  663.   if (event_is_button(cur_event) && event_is_down(cur_event))
  664.     {
  665.            if (id == MS_LEFT)   nextc = LEFT_DOWN ;
  666.       else if (id == MS_MIDDLE) nextc = MIDDLE_DOWN ;
  667.       else if (id == MS_RIGHT)  nextc = RIGHT_DOWN ;
  668.     }
  669.   else if (event_is_button(cur_event) && event_is_up(cur_event))
  670.     {
  671.            if (id == MS_LEFT)   nextc = LEFT_UP ;
  672.       else if (id == MS_MIDDLE) nextc = MIDDLE_UP ;
  673.       else if (id == MS_RIGHT)  nextc = RIGHT_UP ;
  674.     }
  675.   else if (event_is_ascii(cur_event))
  676.     {
  677.       cur_ch = id ;
  678.       nextc = KEYBOARD ;
  679.     }
  680.   else if (id == KBD_DONE || id == LOC_WINEXIT ||
  681.            id == WIN_STOP || id == LOC_RGNEXIT)      nextc = EXIT_WINDOW ;
  682.   else if (id == LOC_WINENTER || id == LOC_RGNENTER) nextc = ENTER_WINDOW ;
  683.   else if (processing == FALSE &&
  684.            (id == LOC_MOVE || id == LOC_DRAG || id == LOC_TRAJECTORY))
  685.     nextc = MOUSE_MOVING ;
  686.   else if (id == WIN_REPAINT)
  687.     {
  688.            if (curwin == W_PANEL) nextc = PANEL_REPAINT ;
  689.       else if (curwin == W_BOARD) nextc = BOARD_REPAINT ;
  690.       else if (curwin == W_HELP)  nextc = HELP_REPAINT ;
  691.       else if (curwin == W_PROPS) nextc = PROPS_REPAINT ;
  692.     }
  693. }
  694.  
  695.  
  696. static Notify_value
  697. quit_proc(client, status)
  698. Notify_client client ;
  699. Destroy_status status ;
  700. {
  701.   KILL(pid, SIGKILL) ;
  702.   exit(0) ;
  703. }
  704.  
  705.  
  706. void
  707. raise_reve()
  708. {
  709.   wmgr_top((int) window_get(gframe, WIN_FD), rootfd) ;
  710. }
  711.  
  712.  
  713. static Notify_value
  714. read_from_fd(client, fd)
  715. Notify_client client ;
  716. register int fd ;
  717. {
  718.        if (fd == socketfd) read_from_sock(fd) ;          /* Remote human. */
  719.   else if (fd == pipe_io[1][0]) read_from_reve(fd) ;     /* Computer. */
  720.   return(NOTIFY_DONE) ;
  721. }
  722.  
  723.  
  724. void
  725. set_cursor(ctype)
  726. enum curtype ctype ;
  727. {
  728.   WINDOW_SET(ccanvas, WIN_CURSOR, cursor[(int) ctype], 0) ;
  729.   WINDOW_SET(gcanvas, WIN_CURSOR, cursor[(int) ctype], 0) ;
  730.   WINDOW_SET(hcanvas, WIN_CURSOR, cursor[(int) ctype], 0) ;
  731.   WINDOW_SET(pcanvas, WIN_CURSOR, cursor[(int) ctype], 0) ;
  732. }
  733.  
  734.  
  735. void
  736. set_frame(wtype, showing)
  737. enum win_type wtype ;
  738. int showing ;
  739. {
  740.   Frame f ;
  741.  
  742.        if (wtype == W_HELP)  f = hframe ;
  743.   else if (wtype == W_PROPS) f = pframe ;
  744.   WINDOW_SET(f, WIN_SHOW, showing, 0) ;
  745. }
  746.  
  747.  
  748. static Notify_value
  749. sigchldcatcher(client, pid, status, rusage)
  750. Notify_client client ;
  751. int pid ;
  752. union wait *status ;
  753. struct rusage *rusage ;
  754. {
  755.   if (WIFEXITED(*status))
  756.     {
  757.       notify_set_input_func(client, NOTIFY_FUNC_NULL, pipe_io[1][0]) ;
  758.       return(NOTIFY_DONE) ;
  759.     }
  760.   return(NOTIFY_IGNORED) ;
  761. }
  762.  
  763.  
  764. /*ARGSUSED*/
  765. void
  766. start_tool(dtype)      /* Display window and start the notifier. */
  767. enum disp_type dtype ;
  768. {
  769.   struct itimerval tval ;
  770.  
  771.   tval.it_interval.tv_usec = 0 ;     /* Force a timing check every second. */
  772.   tval.it_interval.tv_sec  = 1 ;
  773.   tval.it_value.tv_usec    = 0 ;
  774.   tval.it_value.tv_sec     = 1 ;
  775.   notify_set_itimer_func(gframe, check_clock, ITIMER_REAL,
  776.                          &tval, (struct itimerval *) 0) ;
  777.  
  778.   window_fit(cframe) ;
  779.   window_fit(gframe) ;
  780.   window_fit(hframe) ;
  781.   window_fit(pframe) ;
  782.   notify_interpose_destroy_func(gframe, quit_proc) ;
  783.   notify_interpose_destroy_func(cframe, quit_proc) ;
  784.   window_main_loop(gframe) ;
  785. }
  786.